#!/usr/bin/env python
#coding=utf-8

import wx

class PreferencesView(wx.Frame):
    ''' GUI class for a preferences window. '''

    def __init__(self, model):
        wx.Frame.__init__(self, None, title='Preferences')
        self.model = model
        self.create_toolbar()
        self.create_sizer()
        self.create_dbfile_ctrl()
        self.create_offset_ctrl()
        self.create_lang1_ctrl()
        self.create_lang2_ctrl()
        self.create_voice_output_ctrl()
        self.create_program_path()
        self.create_category_label()
        self.create_category_ctrl()
        self.create_buttons()
        self.SetSizerAndFit(self.mainSizer)

    def create_buttons(self):
        ''' Create cancel and save buttons. '''

        self.cancelBtn = wx.Button(self,-1,'Cancel')
        self.saveBtn = wx.Button(self,-1,'Save')
        self.gridSizer.Add(self.cancelBtn)
        self.gridSizer.Add(self.saveBtn)

    def create_category_ctrl(self):
        categories = self.model.get_categories()
        categoryLst = [category.category for category in categories]
        self.defaultCategoryLst = wx.ComboBox(self, -1, choices=categoryLst, size=(110, 25))
        self.gridSizer.Add(self.defaultCategoryLst)


    def create_category_label(self):
        '''Create the label Default Category.'''
        self.categoryLabel = wx.StaticText(self, -1, 'Default Category')
        self.gridSizer.Add(self.categoryLabel)

    def create_dbfile_ctrl(self):
        ''' Show text and input field. '''

        dbfileCtrl = wx.StaticText(self, -1, label = 'Database name')
        self.dbfileInput = wx.TextCtrl(
            self, -1, value='', size=(110,25))
        self.gridSizer.Add(dbfileCtrl)
        self.gridSizer.Add(self.dbfileInput)

    def create_lang1_ctrl(self):
        lang1_1Ctrl =  wx.StaticText(self, -1, label = 'Language 1')
        self.lang1Input = wx.ComboBox(self, -1,
            choices=self.model.get_languages(), size=(110,25))
        self.gridSizer.Add(lang1_1Ctrl)
        self.gridSizer.Add(self.lang1Input)

    def create_lang2_ctrl(self):
        ''' Text and input field for lang2. '''

        lang2Ctrl = wx.StaticText(self, -1, label = 'Language 2')
        self.lang2Input = wx.ComboBox(self, -1,
            choices=self.model.get_languages(), size=(110,25))
        self.gridSizer.Add(lang2Ctrl)
        self.gridSizer.Add(self.lang2Input)

    def create_offset_ctrl(self):
        ''' Show text and input field for editing errorOffset. '''

        offsetCtrl = wx.StaticText(self, -1, label = 'Error offset')
        self.offsetInput = wx.TextCtrl(
            self, -1, value='',  size=(110,25))
        txt = '''Each new vocabulary will get an offset assigned. Example:
You will start with -5. Each time you have entered the correct
translating will reduce the value and vice versa. You do not have
to enter the minus.'''
        self.offsetInput.SetHelpText(txt)
        self.gridSizer.Add(offsetCtrl)
        self.gridSizer.Add(self.offsetInput)

    def create_program_path(self):
        programPathTxt = wx.StaticText(self, -1, label = 'Voice program location')
        self.voicePathCtrl = wx.TextCtrl(self, -1, value='', size=(110,25))
        self.gridSizer.Add(programPathTxt)
        self.gridSizer.Add(self.voicePathCtrl)

    def create_sizer(self):
        ''' Create our frame sizer. '''

        self.mainSizer = wx.BoxSizer(orient=wx.VERTICAL)
        self.gridSizer = wx.FlexGridSizer(4,2,5,5)
        self.mainSizer.Add(self.gridSizer,flag=wx.ALL,border=20)

    def create_toolbar(self):
        ''' Create a toolbar. Mainly for the context help. '''


        self.toolBar = self.CreateToolBar()
        cBtn = wx.ContextHelpButton(self.toolBar)
        self.toolBar.AddControl(cBtn)
        self.toolBar.Realize()

    def create_voice_output_ctrl(self):

        voiceOutputTxt =  wx.StaticText(self, -1, label = 'Voice output')
        self.voiceOutputCtrl = wx.ComboBox(self, -1,
            choices=self.model.get_program_list(), size=(110,25))
        self.gridSizer.Add(voiceOutputTxt)
        self.gridSizer.Add(self.voiceOutputCtrl)

    def get_dbfile(self):
        ''' Returns the name of the database. '''

        return self.dbfileInput.GetValue()

    def get_default_category(self):
        '''Returns the default category selected by the user.'''

        return self.defaultCategoryLst.GetValue()

    def get_lang1(self):
        ''' Returns the name of language 1. '''

        return self.lang1Input.GetValue()

    def get_lang2(self):
        ''' Returns the name of language 2. '''

        return self.lang2Input.GetValue()

    def get_offset(self):
        ''' Returns the value from the error offset.'''

        return self.offsetInput.GetValue()

    def get_voiceOutputPrg(self):
        return self.voiceOutputCtrl.GetValue()

    def get_voice_prg_location(self):
        return self.voicePathCtrl.GetValue()

    def set_category_default(self, value):
        ''' Set value for the default category.'''

        self.defaultCategoryLst.SetStringSelection(value)

    def set_dbfile(self, value):
        ''' Set value from the configuration file. '''

        self.dbfileInput.SetValue(value)

    def set_error_offset(self, value):
        ''' Set value from the configuration file.'''

        self.offsetInput.SetValue(value)

    def set_lang1(self, value):
        ''' Sets the name of language 1. '''

        self.lang1Input.SetValue(value)


    def set_lang2(self, value):
        ''' Sets the name of language 2. '''

        self.lang2Input.SetValue(value)

    def set_voicePrg(self, value):

        self.voiceOutputCtrl.SetValue(value)

    def set_voice_prg_location(self, value):
        self.voicePathCtrl.SetValue(value)

    def show_message(self, msg):
        ''' Wrapper around wx.MessageBox. '''

        wx.MessageBox(str(msg))

    def quit(self):
        ''' Close window without saving changes. '''

        self.Destroy()

    def start(self):
        self.CenterOnScreen()
        self.Show()
